package com.shopoo.oauth.infrastructure.repository;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopoo.exception.BizException;
import com.shopoo.oauth.domain.entity.Role;
import com.shopoo.oauth.domain.repository.RoleRepository;
import com.shopoo.oauth.infrastructure.assembler.InfrastructureAssembler;
import com.shopoo.oauth.infrastructure.converter.InfrastructureConverter;
import com.shopoo.oauth.infrastructure.database.dataobject.OauthRole;
import com.shopoo.oauth.infrastructure.database.mapper.RoleMapper;
import org.apache.commons.lang3.StringUtils;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Repository;

/**
 * @Description 角色仓库
 * @Date 2020/5/24 1:59 下午
 * @Author <a href="mailto:android_li@sina.cn">Joe</a>
 **/
@Repository
public class RoleRepositoryImpl extends ServiceImpl<RoleMapper, OauthRole> implements RoleRepository {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public List<Role> findByUserId(String userId) {
        return roleMapper.findByUserId(userId);
    }

    @Override
    public List<Role> findAllRole(String userId) {
        return roleMapper.findAllRole(userId);
    }

    @Override
    public List<Role> findAllValidRole() {
        LambdaQueryWrapper<OauthRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OauthRole::getStatus, 1);
        queryWrapper.orderByAsc(OauthRole::getRoleId);
        List<OauthRole> list = super.list(queryWrapper);
        return list.stream().map(oauthRole -> {return InfrastructureConverter.INSTANCE.toRole(oauthRole);}).collect(Collectors.toList());
    }

    @Override
    public Set<Long> findRolesByUserId(String userId) {
        List<Role> list = roleMapper.findByUserId(userId);
        return list.stream().map(oauthRole -> {return oauthRole.getRoleId();}).collect(Collectors.toSet());
    }

    @Override
    public List<Role> findByConditions(Role role) {
        LambdaQueryWrapper<OauthRole> queryWrapper = new LambdaQueryWrapper<>();
        if (null != role.getRoleId()) {
            queryWrapper.eq(OauthRole::getRoleId, role.getRoleId());
        }
        if (StringUtils.isNotBlank(role.getName())) {
            queryWrapper.like(OauthRole::getName, role.getName());
        }
        if (StringUtils.isNotBlank(role.getRemark())) {
            queryWrapper.like(OauthRole::getRemark, role.getRemark());
        }
        queryWrapper.orderByDesc(OauthRole::getCreateTime);
        List<OauthRole> list = super.list(queryWrapper);
        return InfrastructureAssembler.converterRoleList(list);
    }

    @Override
    public Role checkName(String name) {
        LambdaQueryWrapper<OauthRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OauthRole::getName, name);
        OauthRole oauthRole = roleMapper.selectOne(queryWrapper);
        return InfrastructureConverter.INSTANCE.toRole(oauthRole);
    }

    @Override
    public void update(Role role) {
        OauthRole oauthRole = InfrastructureConverter.INSTANCE.toOauthRole(role);
        try {
            super.updateById(oauthRole);
        } catch (DuplicateKeyException e) {
            throw new BizException("角色名已存在，请使用其它角色名");
        }
    }

    @Override
    public void save(Role role) {
        OauthRole oauthRole = InfrastructureConverter.INSTANCE.toOauthRole(role);
        super.save(oauthRole);
    }

    @Override
    public void deleteById(Long roleId) {
        super.removeById(roleId);
    }

    @Override
    public Role getById(Long roleId) {
        OauthRole oauthRole = super.getById(roleId);
        return InfrastructureConverter.INSTANCE.toRole(oauthRole);
    }

}
